﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace CommonLibraries.DeviceLibrary
{
    /// <summary>
    /// Interop to external, unmanaged libraries
    /// </summary>
    class TGMS2
    {
        /* GMS2 C# Interop v0.01 by Jerry Nixon, August 6, 07 */

        #region Enumerators
        public struct SYSTEMTIMESTRUCT
        {
            public ushort wDay;
            public ushort wDayOfWeek;
            public ushort wHour;
            public ushort wMilliseconds;
            public ushort wMinute;
            public ushort wMonth;
            public ushort wSecond;
            public ushort wYear;
        }

        public enum TGMS2ENUM
        {
            TGMS2_MAIN_BATTERY = 0x00000000,
            TGMS2_BACKUP_BATTERY = 0x00000001,
            TGMS2_RTC_BATTERY = 0x00000002,
            TGMS2_AC_VOLTAGE = 0x00000003,
            TGMS2_NO_SUSPEND_BOTH_DRIVE = 0x00,
            TGMS2_SUSPEND_BATTERY_DRIVE = 0x01,
            TGMS2_SUSPEND_AC_DRIVE = 0x02,
            TGMS2_SUSPEND_BOTH_DRIVE = 0x03,
            TGMS2_SUSPEND_UNKNOWN = 0x04,
            TGMS2_COMPASS_CALIB_DEFAULT = 0x00000000,
            TGMS2_COMPASS_CALIB_CURRENT = 0x00000001,
            TGMS2_FINDER_OFF = 0x000000,
            TGMS2_FINDER_ON = 0x000001,
            TGMS2_OV_SXGA = 0x00000000,
            TGMS2_OV_VGA = 0x00000001,
            TGMS2_OV_QVGA = 0x00000002,
            TGMS2_OV_QQVGA = 0x00000003,
            TGMS2_PICTURE_FORMAT_RGB = 0x00000000,
            TGMS2_PICTURE_FORMAT_YUV = 0x00000001,
            TGMS2_CAMERA_GAIN_DAY = 0x00000000,
            TGMS2_CAMERA_GAIN_NIGHT = 0x00000001,
            TGMS2_CAMERA_GAIN_AUTO = 0x00000002,
            TGMS2_CL_TURBO = 0x00,
            TGMS2_CL_416 = 0x01,
            TGMS2_CL_312 = 0x02,
        }

        public enum ERR
        {
            ERR_SUCCESS = 1,
            ERR_ERROR = 0,
            ERR_NOCOFFILE = -1,
            ERR_ATMAGNETICPOLE = -2,
            ERR_ATGEOGRAPHICPOLE = -3,
            ERR_INVALIDVALUES = -4,
            ERR_NONSEVEREWARNING = -5,
            ERR_SEVEREWARNING = -6,
            ERR_INVALIDTIME = -7,
        }
        #endregion Enumerators

        #region  Constants
        
        #region TGMS2 DLL
        // #define TGMS2_CL_RUN 0x03
        public const int TGMS2_CL_RUN = 0x03;

        // #define TGMS2_DLL_FILE_NAME _T("TGMS2.dll")
        public const string TGMS2_DLL_FILE_NAME = "TGMS2.dll";
        
        // #define TGMS2_GETBATTERYPOWERVALUE _T("TGMS2_GetBatteryPowerValue")
        public const string TGMS2_GETBATTERYPOWERVALUE = "TGMS2_GetBatteryPowerValue";

        // #define TGMS2_GETBATTERYCOVERSTATE _T("TGMS2_GetBatteryCoverState")
        public const string TGMS2_GETBATTERYCOVERSTATE = "TGMS2_GetBatteryCoverState";

        // #define TGMS2_SETBATTERYCOVEROPENCHARGE _T("TGMS2_SetBatteryCoverOpenCharge")
        public const string TGMS2_SETBATTERYCOVEROPENCHARGE = "TGMS2_SetBatteryCoverOpenCharge";

        // #define TGMS2_GETBATTERYCOVEROPENCHARGE _T("TGMS2_GetBatteryCoverOpenCharge")
        public const string TGMS2_GETBATTERYCOVEROPENCHARGE = "TGMS2_GetBatteryCoverOpenCharge";

        // #define TGMS2_SETBATTERYCOVEROPENSUSPEND _T("TGMS2_SetBatteryCoverOpenSuspend")
        public const string TGMS2_SETBATTERYCOVEROPENSUSPEND = "TGMS2_SetBatteryCoverOpenSuspend";

        // #define TGMS2_GETBATTERYCOVEROPENSUSPEND _T("TGMS2_GetBatteryCoverOpenSuspend")
        public const string TGMS2_GETBATTERYCOVEROPENSUSPEND = "TGMS2_GetBatteryCoverOpenSuspend";

        // #define TGMS2_SETGPSPOWERSTATE _T("TGMS2_SetGpsPowerState")
        public const string TGMS2_SETGPSPOWERSTATE = "TGMS2_SetGpsPowerState";

        // #define TGMS2_GETGPSPOWERSTATE _T("TGMS2_GetGpsPowerState")
        public const string TGMS2_GETGPSPOWERSTATE = "TGMS2_GetGpsPowerState";

        // #define TGMS2_GPSRESET _T("TGMS2_GPSReset")
        public const string TGMS2_GPSRESET = "TGMS2_GPSReset";

        // #define TGMS2_GPSNVCLEAR _T("TGMS2_GPSNvClear")// 060511 add
        public const string TGMS2_GPSNVCLEAR = "TGMS2_GPSNvClear";

        // #define TGMS2_GETEXTERNALRTC _T("TGMS2_GetExternalRtc")
        public const string TGMS2_GETEXTERNALRTC = "TGMS2_GetExternalRtc";

        // #define TGMS2_SETCOMPASSPOWERSTATE _T("TGMS2_SetCompassPowerState")
        public const string TGMS2_SETCOMPASSPOWERSTATE = "TGMS2_SetCompassPowerState";

        // #define TGMS2_GETCOMPASSPOWERSTATE _T("TGMS2_GetCompassPowerState")
        public const string TGMS2_GETCOMPASSPOWERSTATE = "TGMS2_GetCompassPowerState";

        // #define TGMS2_GETCOMPASSVALUE _T("TGMS2_GetCompassValue")
        public const string TGMS2_GETCOMPASSVALUE = "TGMS2_GetCompassValue";

        // #define TGMS2_SETCOMPASSCALIBRATION _T("TGMS2_SetCompassCalibration")
        public const string TGMS2_SETCOMPASSCALIBRATION = "TGMS2_SetCompassCalibration";

        // #define TGMS2_GETCOMPASSCALIBRATIONDATA _T("TGMS2_GetCompassCalibrationData")
        public const string TGMS2_GETCOMPASSCALIBRATIONDATA = "TGMS2_GetCompassCalibrationData";

        // #define TGMS2_GETTILTVALUE _T("TGMS2_GetTiltValue")
        public const string TGMS2_GETTILTVALUE = "TGMS2_GetTiltValue";

        // #define TGMS2_SETCAMERAPOWERSTATE _T("TGMS2_SetCameraPowerState")
        public const string TGMS2_SETCAMERAPOWERSTATE = "TGMS2_SetCameraPowerState";

        // #define TGMS2_GETCAMERAPOWERSTATE _T("TGMS2_GetCameraPowerState")
        public const string TGMS2_GETCAMERAPOWERSTATE = "TGMS2_GetCameraPowerState";

        // #define TGMS2_SETCAMERAFINDER _T("TGMS2_SetCameraFinder")
        public const string TGMS2_SETCAMERAFINDER = "TGMS2_SetCameraFinder";

        // #define TGMS2_SETCAMERAFINDERZOOM _T("TGMS2_SetCameraFinderZoom")
        public const string TGMS2_SETCAMERAFINDERZOOM = "TGMS2_SetCameraFinderZoom";

        // #define TGMS2_GETCAMERAPICTUREYDATA _T("TGMS2_GetCameraPictureYData")
        public const string TGMS2_GETCAMERAPICTUREYDATA = "TGMS2_GetCameraPictureYData";

        // #define TGMS2_GETCAMERAPICTURECOLORDATA _T("TGMS2_GetCameraPictureColorData")
        public const string TGMS2_GETCAMERAPICTURECOLORDATA = "TGMS2_GetCameraPictureColorData";

        // #define TGMS2_SETCAMERABRIGHT _T("TGMS2_SetCameraBright")
        public const string TGMS2_SETCAMERABRIGHT = "TGMS2_SetCameraBright";

        // #define TGMS2_GETCAMERABRIGHT _T("TGMS2_GetCameraBright")
        public const string TGMS2_GETCAMERABRIGHT = "TGMS2_GetCameraBright";

        // #define TGMS2_SETCAMERASHARP _T("TGMS2_SetCameraSharp")
        public const string TGMS2_SETCAMERASHARP = "TGMS2_SetCameraSharp";

        // #define TGMS2_GETCAMERASHARP _T("TGMS2_GetCameraSharp")
        public const string TGMS2_GETCAMERASHARP = "TGMS2_GetCameraSharp";

        // #define TGMS2_SETCAMERAAGAINSTSUN _T("TGMS2_SetCameraAgainstSun")
        public const string TGMS2_SETCAMERAAGAINSTSUN = "TGMS2_SetCameraAgainstSun";

        // #define TGMS2_GETCAMERAAGAINSTSUN _T("TGMS2_GetCameraAgainstSun")
        public const string TGMS2_GETCAMERAAGAINSTSUN = "TGMS2_GetCameraAgainstSun";

        // #define TGMS2_SETCAMERANIGHTMODE _T("TGMS2_SetCameraNightMode")
        public const string TGMS2_SETCAMERANIGHTMODE = "TGMS2_SetCameraNightMode";

        // #define TGMS2_GETCAMERANIGHTMODE _T("TGMS2_GetCameraNightMode")
        public const string TGMS2_GETCAMERANIGHTMODE = "TGMS2_GetCameraNightMode";

        // #define TGMS2_SETDRAWIMAGETOOVERLAY _T("TGMS2_SetDrawImageToOverlay")
        public const string TGMS2_SETDRAWIMAGETOOVERLAY = "TGMS2_SetDrawImageToOverlay";

        // #define TGMS2_OFFDRAWIMAGETOOVERLAY _T("TGMS2_OFFDrawImageToOverlay")
        public const string TGMS2_OFFDRAWIMAGETOOVERLAY = "TGMS2_OFFDrawImageToOverlay";

        // #define TGMS2_SETSHUTTERSOUNDFILE _T("TGMS2_SetShutterSoundFile")
        public const string TGMS2_SETSHUTTERSOUNDFILE = "TGMS2_SetShutterSoundFile";

        // #define TGMS2_SETFREQMODE _T("TGMS2_SetFreqMode")
        public const string TGMS2_SETFREQMODE = "TGMS2_SetFreqMode";

        // #define TGMS2_GETFREQMODE _T("TGMS2_GetFreqMode")
        public const string TGMS2_GETFREQMODE = "TGMS2_GetFreqMode";

        // #define TGMS2_GETSERIALNUMBER _T("TGMS2_GetSerialNumber")
        public const string TGMS2_GETSERIALNUMBER = "TGMS2_GetSerialNumber";

        // #define TGMS2_COMPUTEDECLINATION _T("TGMS2_ComputeDeclination")
        public const string TGMS2_COMPUTEDECLINATION = "TGMS2_ComputeDeclination";
        #endregion TGMS2 DLL

        #region TGMS2HELPER DLL
        public const string TGMS2HELPER_DLL_FILE_NAME = "TGMS2HELPER.dll";
        #endregion
        
        #endregion



        #region Power unit Library specification

        // BOOL TGMS2_GetBatteryPowerValue(DWORD dwBattery, LPDWORD pdwVoltage);
        /// <summary>
        ///     It acquires the voltage of battery.
        /// </summary>
        /// <param name="dwBattery">It specifies the kind of the battery.</param>
        /// <param name="pdwVoltage">It specifies the pointer to the variable by receives the voltage value.</param>
        /// <returns>
        ///     Succeed: The values other than 0 return.
        ///     Fail: The values 0 return.
        /// </returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_GetBatteryPowerValue(uint dwBattery, ref int pdwVoltage);

        // BOOL TGMS2_GetBatteryCoverState(VOID);
        /// <summary>
        ///     It acquires the stats of the battery cover.
        /// </summary>
        /// <returns>
        ///     TRUE: The battery cover is closed.
        ///     FALSE: The battery cover is opened.
        /// </returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_GetBatteryCoverState();

        // BOOL TGMS2_SetBatteryCoverOpenCharge(BOOL bEnableCharge);
        /// <summary>
        ///     It sets the charge operation when the battery cover opens.
        /// </summary>
        /// <param name="bEnableCharge">
        ///     It specifies the charge operation when the battery cover opens.
        ///     TRUE: even if the battery cover opens, it can charge.
        ///     FALSE: when the battery cover opens, it cannot charge. (Default)
        /// </param>
        /// <returns></returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_SetBatteryCoverOpenCharge(int bEnableCharge);

        // BOOL TGMS2_GetBatteryCoverOpenCharge(VOID);
        /// <summary>
        /// It acquires the stats of charge operation when the battery cover opens.
        /// </summary>
        /// <returns>
        ///     TRUE: Even if the battery cover opens, it can charge.
        ///     FALSE: When the battery cover opens, it cannot charge. 
        ///</returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_GetBatteryCoverOpenCharge();

        // BOOL TGMS2_SetBatteryCoverOpenSuspend(UCHAR ucEnableSuspend);
        /// <summary>
        /// It sets the suspend operation when the battery cover opens.
        /// </summary>
        /// <param name="ucEnableSuspend">It specifies the suspend operation when the battery cover opens.</param>
        /// <returns>
        ///         Succeed: The values other than 0 return.
        ///         Fail: The values 0 return.
        /// </returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_SetBatteryCoverOpenSuspend(byte ucEnableSuspend);

        // UCHAR TGMS2_GetBatteryCoverOpenSuspend(VOID);
        /// <summary>
        /// It acquires the stats of suspend operation when the battery cover opens.
        /// </summary>
        /// <returns>
        ///     Succeed: It returns the suspend operation when the battery cover opens.
        ///     Fail: The values 0 return.
        ///     It uses the GetLastError function to acquire the enhancing error. 
        ///</returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern byte TGMS2_GetBatteryCoverOpenSuspend();
        #endregion

        #region GPS unit Library specification

        // BOOL TGMS2_SetGpsPowerState(BOOL bGpsPower);
        /// <summary>
        /// It sets the state of power supply of GPS unite.
        /// </summary>
        /// <param name="bGpsPower">
        ///     It sets the state of the power supply for GPS unite.
        ///     TRUE: It turns ON the power supply of the GPS unite.
        ///     FALSE: It turns OFF the power supply of the GPS unite. (Default)
        /// </param>
        /// <returns>
        ///     Succeed: The values other than 0 return.
        ///     Fail: The values 0 return. 
        ///</returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_SetGpsPowerState(int bGpsPower);

        // BOOL TGMS2_GetGpsPowerState(VOID);
        /// <summary>
        /// It acquires the state of power supply of GPS unite.
        /// </summary>
        /// <returns>
        ///     TRUE: It is power supplies of the GPS unite.
        ///     FALSE: It is not power supplies of the GPS unite. (Default) 
        ///</returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_GetGpsPowerState();

        // BOOL TGMS2_GPSReset(VOID);
        /// <summary>
        /// It resets the GPS unit.
        /// </summary>
        /// <returns>
        ///     TRUE: It succeeded in reset of the GPS unit.
        ///     FALSE: It fails in reset of the GPS unit. 
        ///</returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_GPSReset();

        // BOOL TGMS2_GPSNvClear(VOID);// 060511 add
        /// <summary>
        /// It initials the NVRAM of GPS unit.
        /// </summary>
        /// <returns>
        ///     TRUE: It succeeded in initial the NVRAM of the GPS unit.
        ///     FALSE: It fails in initial the NVRAM of the GPS unit. 
        ///</returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_GPSNvClear();

        #endregion 

        #region Real Time Clock Library Specification
        // BOOL TGMS2_GetExternalRtc(LPSYSTEMTIME pGetTime);
        /// <summary>
        /// It acquires the external RTC time (year, month, day of week, day, hour, minute, second)
        /// </summary>
        /// <param name="pGetTime">
        ///     It is a structure on the time of Windows standard.
        ///     It specifies the pointer of a structural of the time to receive the value acquired from external RTC.
        /// </param>
        /// <returns>
        ///     Succeed: The values other than 0 return.
        ///     Fail: The values 0 return. 
        ///</returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_GetExternalRtc(SYSTEMTIMESTRUCT pGetTime);
        #endregion

        #region Compass / Tilt Unit Library Specification

        // BOOL TGMS2_SetCompassPowerState(BOOL bCompassPower);
        /// <summary>
        /// It sets the state of power supply of Compass and Tilt unit.
        /// </summary>
        /// <param name="bCompassPower">
        ///     It sets the state of the power supply of Compass and Tilt unit.
        ///     TRUE: It turns ON the power supply of the Compass and Tilt unit.
        ///     FALSE: It turns OFF the power supply of the Compass and Tilt unit.
        /// </param>
        /// <returns>
        ///     Succeed: The values other than 0 return.
        ///     Fail: The values 0 return.
        ///</returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_SetCompassPowerState(int bCompassPower);

        // BOOL TGMS2_GetCompassPowerState(VOID);
        /// <summary>
        /// It acquires the state of power supply of Compass and Tilt unit.
        /// </summary>
        /// <returns>
        ///     TRUE: It is power supplies of the Compass and Tilt unit.
        ///     FALSE: It is not power supplies of the Compass and Tilt unit. 
        ///</returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_GetCompassPowerState();

        // BOOL TGMS2_GetCompassValue(PFLOAT pfAngle, DWORD dwGetCount=1);
        /// <summary>
        /// It acquires the data of the compass unit.
        /// </summary>
        /// <param name="pfAngle">It acquires angle Value base on the magnetic north. (The unit is a radian)</param>
        /// <param name="dwGetCount">
        ///     It specifies number (1-10) of samples of data.
        ///     It calculates the average of the specified number of samples as an angle.
        ///     The compass data is updated to 100ms.
        ///     Therefore, if it specifies “10”, an angle value is returned 1 second later.
        /// </param>
        /// <returns>
        ///     Succeed: The values other than 0 return.
        ///     Fail: The values 0 return. 
        ///</returns>
        ///<note>
        ///*    When “TGMS2_SetCompassCalibration” normally initializes the compass unit, it calculates the angle by
        ///     using the calibration parameters.
        ///*    When it does not do the calibration, it uses calibration parameters done when it is the factory.
        ///     Therefore, the angle might not be normally calculated.
        ///*    The accuracy of the compass is division into 16.
        ///</note>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_GetCompassValue(ref float pfAngle, uint dwGetCount);

        // BOOL TGMS2_SetCompassCalibration(BOOL bCompassCalib);
        /// <summary>
        /// It initializes the compass unit.
        /// </summary>
        /// <param name="bCompassCalib">
        ///     It sets the calibration of the compass unit.
        ///     Calibration makes one rotation with GMS2 horizontally in the place used.
        ///     TRUE: Calibration starts.
        ///     FALSE: Calibration stops.
        /// </param>
        /// <returns>
        ///     Succeed: The values other than 0 return.
        ///     Fail: The values 0 return.
        ///</returns>
        ///<note>
        ///*    Please do the initialization work within 60 seconds.
        ///*    Initialization is a function because it excludes a magnetic influence of the surrounding in the place where
        ///     uses the compass.
        ///*    Therefore, it cannot accurately calculate the angle if it does not initialize.
        ///*    This magnetic sensor is done to detect a magnetic influence of surroundings sensitively. Therefore, it might
        ///     not calculate an accurate angle even if it initializes the compass.
        ///</note>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_SetCompassCalibration(int bCompassCalib);

        // BOOL TGMS2_GetCompassCalibrationData(
        // PFLOATpfXgain,
        // PFLOATpfYgain,
        // LPDWORD pdwXset_Dc,
        // LPDWORD pdwXreset_Dc,
        // LPDWORD pdwYset_Dc,
        // LPDWORD pdwYreset_Dc,
        // DWORD dwCalibData
        // );

        /// <summary>
        ///     It acquires the calibration parameter of the compass unit.
        /// </summary>
        /// <param name="pfXgain">X element Gain Value</param>
        /// <param name="pfYgain">Y element Gain Value</param>
        /// <param name="pdwXset_Dc">X element SET_DC value</param>
        /// <param name="pdwXreset_Dc">X element RESET_DC value</param>
        /// <param name="pdwYset_Dc">Y element SET_DC value</param>
        /// <param name="pdwYreset_Dc">Y element SET_DC value</param>
        /// <param name="dwCalibData">It selects the value the factory or currently parameters.</param>
        /// <returns>
        ///     Succeed: The values other than 0 return.
        ///     Fail: The values 0 return. 
        ///</returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_GetCompassCalibrationData(ref float pfXgain, ref float pfYgain, ref int pdwXset_Dc, ref int pdwXreset_Dc, ref int pdwYset_Dc, ref int pdwYreset_Dc, uint dwCalibData);

        // BOOL TGMS2_GetTiltValue(PFLOAT pfXangle, PFLOAT pfYangle, DWORD dwGetCount=1);
        /// <summary>
        /// It acquires the data of the tilt unit.
        /// </summary>
        /// <param name="pfXangle">Horizontal state angle is Zero, “-“ means the left side and “+” means the right side.</param>
        /// <param name="pfYangle">Horizontal state angle is Zero, “-“ means the up side and “+” means the bottom side.</param>
        /// <param name="dwGetCount">
        ///     It specifies number (1-10) of samples of data.
        ///     It calculates the average of the specified number of samples as an angle.
        ///     The compass data is updated to 100ms.
        ///     Therefore, if it specifies “10”, an angle value is returned 1 second later.
        /// </param>
        /// <returns>
        ///     Succeed: The values other than 0 return.
        ///     Fail: The values 0 return.
        ///</returns>
        ///<note>
        ///*    Resolution is every 0.1 degrees.
        ///*    Accuracy becomes ± two degrees.
        ///*    The measurement up to 90 degrees or less is possible.
        ///     It recommends about 40 degrees to be used in accuracy as the maximum value.
        ///</note>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_GetTiltValue(ref float pfXangle, ref float pfYangle, uint dwGetCount);

        // LONG TGMS2_ComputeDeclination(FLOAT ALat, FLOAT ALon, FLOAT AHgt, SYSTEMTIME ADate, PFLOAT ADec, PFLOAT ADip, PFLOAT ATI);
        /// <summary>
        /// It acquires the data of the magnetic declination.
        /// </summary>
        /// <param name="ALat">It specifies latitude at the position where it calculates the magnetic declination. (WGS84). The unit is degree.</param>
        /// <param name="ALon">It specifies longitude at the position where it calculates the magnetic declination. (WGS84). The unit is degree.</param>
        /// <param name="AHgt">It specifies height at the position where it calculates the magnetic declination. (WGS84). The unit is meter.</param>
        /// <param name="ADate">It specifies local time that it calculates the magnetic declination.</param>
        /// <param name="ADec">
        ///     It specifies the pointer of the magnetic declination angle value. The unit is degree.
        ///     When magnetic north is east of the true north, the declination is positive.
        ///     If it obtains the true north, the declination must be added to the compass value.
        /// </param>
        /// <param name="ADip">
        ///     It specifies the pointer of the Inclination of magnetic plane. The unit is degree.
        ///     The positive means the angle between the horizontal plane and the field vector.
        /// </param>
        /// <param name="ATI">It specifies the pointer of the magnetic plane intense.</param>
        /// <returns>
        ///     Succeed: The values 1 return.
        ///     Fail: The values 0 return
        ///     Other value: see Enum ERR.
        ///</returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_ComputeDeclination(float ALat, float ALon, float AHgt, SYSTEMTIMESTRUCT ADate, ref float ADec, ref float ADip, ref float ATI);
        #endregion

        #region Camera Library Unit Specification

        /// <summary>
        /// Call TGMS2HELPER.dll and Capture the image and save with Filename
        /// </summary>
        /// <param name="Size">0 TGMS2_OV_SXGA,
        ///                    1 TGMS2_OV_VGA,
        ///                    2 TGMS2_OV_QVGA,
        ///                    3 TGMS2_OV_QQVGA
        ///                    </param>
        /// <param name="FileName">the name of the image</param>
        /// <returns></returns>
        [DllImport(TGMS2HELPER_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int CaptureImage(int Size, string FileName);

        // BOOL TGMS2_SetCameraPowerState(BOOL bCameraPower);
        /// <summary>
        /// It sets the state of power supply of Camera unit.
        /// </summary>
        /// <param name="bCameraPower">
        ///     It sets the state of the power supply of Camera unit.
        ///     TRUE: It turns ON the power supply of the Camera unite.
        ///     FALSE: It turns OFF the power supply of the Camera unite.
        /// </param>
        /// <returns>
        ///     Succeed: The values other than 0 return.
        ///     Fail: The values 0 return.
        ///</returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_SetCameraPowerState(int bCameraPower);

        // BOOL TGMS2_GetCameraPowerState(VOID);
        /// <summary>
        ///     It acquires the state of power supply of Camera unit.
        /// </summary>
        /// <returns>
        ///     TRUE: It is power supplies of the Camera unit.
        ///     FALSE: It is not power supplies of the Camera unit. 
        ///</returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_GetCameraPowerState();

        // BOOL TGMS2_SetCameraFinder(BOOL bState, DWORD dwXPoint=0, DWORD dwYPoint=0);
        /// <summary>
        ///     It displays at the position where it specified camera image (QQVGA).
        /// </summary>
        /// <param name="bState">
        ///     It specifies if to display the camera.
        ///         TGMS2_FINDER_ON  -> Displays the image.
        ///         TGMS2_FINDER_OFF -> Does not display the image.
        /// </param>
        /// <param name="dwXPoint">X coordinates at display position specification (Default : 0)</param>
        /// <param name="dwYPoint">Y coordinates at display position specification (Default : 0)</param>
        /// <returns>
        ///     Succeed: The values other than 0 return.
        ///     Fail: The values 0 return. 
        ///</returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_SetCameraFinder(int bState, uint dwXPoint, uint dwYPoint);


        // BOOL TGMS2_GetCameraPictureYData(PBYTE pucData, DWORD dwImageSize,DWORD dwLineFilter);
        /// <summary>
        /// It acquires the data of the gray element of the display image.
        /// </summary>
        /// <param name="pucData">It specifies the pointer of the image data.</param>
        /// <param name="dwImageSize">
        ///     It specifies the acquired image size.
        ///         TGMS2_OV_SXGA Data Size1280x1024
        ///         TGMS2_OV_VGA Data Size640x480
        ///         TGMS2_OV_QVGA Data Size320x240
        ///         TGMS2_OV_QQVGA Data Size160x120
        /// </param>
        /// <param name="dwLineFilter">
        /// It specifies filtering the acquired image data.
        ///     1 Filter
        ///     0 Does not filter
        /// </param>
        /// <returns>
        ///     Succeed: The values other than 0 return.
        ///     Fail: The values 0 return. 
        ///</returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_GetCameraPictureYData(ref IntPtr pucData, uint dwImageSize, uint dwLineFilter);

        // BOOL TGMS2_GetCameraPictureColorData(
        // PUSHORT pusData,
        // DWORD dwImageSize,
        // DWORD dwdataFormat,
        // DWORD dwLineFilter
        // );
        /// <summary>
        /// It acquires the display image.
        /// </summary>
        /// <param name="pusData">It specifies the pointer of the image data.</param>
        /// <param name="dwImageSize">
        ///     It specifies the acquired image size.
        ///         TGMS2_OV_SXGA Data Size1280x1024
        ///         TGMS2_OV_VGA Data Size640x480
        ///         TGMS2_OV_QVGA Data Size320x240
        ///         TGMS2_OV_QQVGA Data Size160x120
        /// </param>
        /// <param name="dwdataFormat">
        ///     It specifies the acquired data format.
        ///         TGMS2_PICTURE_FORMAT_RGB RGB
        ///         TGMS2_PICTURE_FORMAT_YUV YUV
        /// </param>
        /// <param name="dwLineFilter">
        ///     It specifies filtering the acquired image data.
        ///         1 Filter
        ///         0 Does not filter
        /// </param>
        /// <returns></returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
       //public static extern int TGMS2_GetCameraPictureColorData(ref ushort[] pusData, uint dwImageSize, uint dwdataFormat, uint dwLineFilter);
       //public static extern int TGMS2_GetCameraPictureColorData(IntPtr pusData, uint dwImageSize, uint dwdataFormat, uint dwLineFilter);
        public static extern int TGMS2_GetCameraPictureColorData(byte[] pusData, uint dwImageSize, uint dwdataFormat, uint dwLineFilter);
        //unsafe public static extern int TGMS2_GetCameraPictureColorData(System.IO.MemoryStream pusData, uint dwImageSize, uint dwdataFormat, uint dwLineFilter);

        // BOOL TGMS2_SetCameraBright(LONG lLevel);
        /// <summary>
        /// It sets the Bright value of the camera.
        /// </summary>
        /// <param name="lLevel">It specifies setting Value of brightness. (-5 to 5)</param>
        /// <returns>
        ///     Succeed: The values other than 0 return.
        ///     Fail: The values 0 return. 
        ///</returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_SetCameraBright(int lLevel);

        // BOOL TGMS2_GetCameraBright(PLONG plLevel);
        /// <summary>
        /// It acquires the Bright value of the camera.
        /// </summary>
        /// <param name="plLevel">It means setting Value of brightness. (-5 to 5)</param>
        /// <returns>
        ///     Succeed: The values other than 0 return.
        ///     Fail: The values 0 return. 
        ///</returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_GetCameraBright(ref int plLevel);

        // BOOL TGMS2_SetCameraSharp(DWORD dwLevel);
        /// <summary>
        /// It sets the Edge value of the camera.
        /// </summary>
        /// <param name="dwLevel"> It specifies setting Value of Edge. (0to 6) </param>
        /// <returns>
        ///     Succeed: The values other than 0 return.
        ///     Fail: The values 0 return. 
        ///</returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_SetCameraSharp(uint dwLevel);

        // BOOL TGMS2_GetCameraSharp(LPDWORD pdwLevel);
        /// <summary>
        /// It acquires the Edge value of the camera.
        /// </summary>
        /// <param name="pdwLevel"> It means setting Value of Edge. (0to 6) </param>
        /// <returns>
        ///     Succeed: The values other than 0 return.
        ///     Fail: The values 0 return. 
        ///</returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_GetCameraSharp(ref int pdwLevel);

        // BOOL TGMS2_SetCameraAgainstSun(BOOL bAgainstSun);
        /// <summary>
        /// It sets the against sun function of the camera.
        /// </summary>
        /// <param name="bAgainstSun">
        /// It specifies the setting of the againstsun.
        ///    TRUE: It sets the againstsun of the camera unit.
        ///    FALSE: It releases the againstsun of the camera unit.
        /// </param>
        /// <returns>
        ///     Succeed: The values other than 0 return.
        ///     Fail: The values 0 return. 
        ///</returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_SetCameraAgainstSun(int bAgainstSun);

        // BOOL TGMS2_GetCameraAgainstSun(VOID);
        /// <summary>
        /// It acquires the against sun function of the camera.
        /// </summary>
        /// <returns>
        ///     TRUE: Againstsun is set.
        ///     FALSE: Againstsun is not set. 
        ///</returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_GetCameraAgainstSun();


        // BOOL TGMS2_SetCameraNightMode(DWORD dwLevel);
        /// <summary>
        /// It sets the mode at night mode of the camera unit.
        /// </summary>
        /// <param name="dwLevel">
        ///     It specifies the night mode of the camera unit.
        ///         TGMS2_CAMERA_GAIN_DAY Normal mode (Default)
        ///         TGMS2_CAMERA_GAIN_NIGHT Night mode
        ///         TGMS2_CAMERA_GAIN_AUTO Auto mode
        /// </param>
        /// <returns>
        ///     Succeed: The values other than 0 return.
        ///     Fail: The values 0 return. 
        ///</returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_SetCameraNightMode(uint dwLevel);

        // BOOL TGMS2_GetCameraNightMode(LPDWORD pdwLevel);
        /// <summary>
        /// It acquires the mode at night mode of the camera unit.
        /// </summary>
        /// <param name="dwLevel">
        ///     It specifies the night mode of the camera unit.
        ///         TGMS2_CAMERA_GAIN_DAY Normal mode (Default)
        ///         TGMS2_CAMERA_GAIN_NIGHT Night mode
        ///         TGMS2_CAMERA_GAIN_AUTO Auto mode
        /// </param>
        /// <returns>
        ///     Succeed: The values other than 0 return.
        ///     Fail: The values 0 return. 
        ///</returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_GetCameraNightMode(ref int pdwLevel);


        // BOOL TGMS2_SetDrawImageToOverlay(
        // PVOID pImageData,
        // DWORD dwXPoint,
        // DWORD dwYPoint,
        // DWORD dwWidthSize,
        // DWORD dwHeightSize
        // );
        /// <summary>
        /// It draws to specify the Overlay in an arbitrary image.
        /// </summary>
        /// <param name="pImageData">Specification of pointer to image data. The format of the data of the image is RGB16</param>
        /// <param name="dwXPoint">Drawing starting position (X coordinate)</param>
        /// <param name="dwYPoint">Drawing starting position (Y coordinate) </param>
        /// <param name="dwWidthSize">Width of drawing image</param>
        /// <param name="dwHeightSize">Height of drawing image</param>
        /// <returns>
        ///     Succeed: The values other than 0 return.
        ///     Fail: The values 0 return. 
        ///</returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_SetDrawImageToOverlay(IntPtr pImageData, uint dwXPoint, uint dwYPoint, uint dwWidthSize, uint dwHeightSize);

        // BOOL TGMS2_OFFDrawImageToOverlay(VOID);
        /// <summary>
        /// It deletes the image where it is being drawn by Overlay
        /// </summary>
        /// <returns>
        ///     Succeed: The values other than 0 return.
        ///     Fail: The values 0 return. 
        ///</returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_OFFDrawImageToOverlay();

        // BOOL TGMS2_SetShutterSoundFile(CString sSoundFilePath, int nSound = 0 );
        /// <summary>
        /// It sets the shutter sound when capturing the camera image.
        /// </summary>
        /// <param name="sSoundFilePath">Set passes the shutter sound file. Only “*.wav” file set.</param>
        /// <param name="nSound">
        ///     Set 2 wav files or less.
        ///         0 Sounds one wav file while capturing the camera image.
        ///         1 Sounds two wav files while capturing the camera image.
        /// </param>
        /// <returns>
        ///     TRUE: It is able to read the sound file.
        ///     FALSE: it is not able to read the sound file. 
        ///</returns>
        ///<note>
        ///*    The sound file specified when it acquires the image sounds using the TGMS2_GetCameraPictureYData,
        ///     TGMS2_GetCameraPictureColorData.
        ///</note>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_SetShutterSoundFile(String sSoundFilePath, int nSound);


        // BOOL TGMS2_SetCameraFinderZoom(DWORD dwZoomSize);
        /// <summary>
        /// It does the zoom-in and zoom-out by the size for which the camera image is specified.
        /// </summary>
        /// <param name="dwZoomSize">
        ///     Specified the size.
        ///         TGMS2_OV_QQVGA X1 Zoom
        ///         TGMS2_OV_QVGA X2 Zoom
        ///         TGMS2_OV_VGA X4 Zoom
        /// </param>
        /// <returns>
        ///     Succeed: The values other than 0 return.
        ///     Fail: The values 0 return. 
        ///</returns>
        ///<note>
        ///     When it operates TGMS2_GetCameraPictureYData and TGMS2_GetCameraPictureColorData by the
        ///     settings X2 and X4 zoom, it might not be able to acquire the image normally.
        ///     Be sure to use TGMS2_GetCameraPictureYData and TGMS2_GetCameraPictureColorData by X1 zoom.
        ///</note>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_SetCameraFinderZoom(uint dwZoomSize);

        #endregion

        #region PXA270 Operation Frequency Control Library Specification
        // BOOL TGMS2_SetFreqMode(UCHAR ucFreq,BOOL bReboot);
        /// <summary>
        /// It changes the operation frequency of PXA270.
        /// </summary>
        /// <param name="ucFreq">
        /// It specifies the operation frequency of PXA270.
        ///     TGMS2_CL_TURBO Operation frequency is 520MHz
        ///     TGMS2_CL_416 Operation frequency is 416MHz
        ///     TGMS2_CL_312 Operation frequency is 312MHz
        ///     TGMS2_CL_RUN Operation frequency is 208MHz
        /// </param>
        /// <param name="bReboot">
        /// It specifies if to warm boot after it sets the operation frequency.
        ///     0 Does not warm boot
        ///     1 Warm boot
        /// </param>
        /// <returns>
        ///     Succeed: The values other than 0 return.
        ///     Fail: The values 0 return. 
        ///</returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_SetFreqMode(byte ucFreq, int bReboot);

        // BOOL TGMS2_GetFreqMode(PUCHAR pucFreq);
        /// <summary>
        /// It acquires the operation frequency of PXA270.
        /// </summary>
        /// <param name="pucFreq">
        /// It specifies the pointer to the variable by receives the PXA270 operation frequency value.
        ///     TGMS2_CL_TURBO Operation frequency is 520MHz
        ///     TGMS2_CL_416 Operation frequency is 416MHz
        ///     TGMS2_CL_312 Operation frequency is 312MHz
        ///     TGMS2_CL_RUN Operation frequency is 208MHz
        /// </param>
        /// <returns>
        ///     Succeed: The values other than 0 return.
        ///     Fail: The values 0 return. 
        ///</returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_GetFreqMode(ref byte pucFreq);

        #endregion

        #region Flash Rom control Library Specification
        // BOOL TGMS2_GetSerialNumber(PCHAR szSerialNumber);
        /// <summary>
        ///     It acquires the GMS2’s serial number.
        /// </summary>
        /// <param name="szSerialNumber">
        /// It specifies the pointer to the serial number. The number of characters of serial number is 12 characters.
        /// </param>
        /// <returns>
        ///     Succeed: The values other than 0 return.
        ///     Fail: The values 0 return. 
        ///</returns>
        [DllImport(TGMS2_DLL_FILE_NAME, CharSet = CharSet.Unicode)]
        public static extern int TGMS2_GetSerialNumber(byte szSerialNumber);
        #endregion

    }
}
